package com.cnhis.cloudhealth.clinical.infusion.base.dao;

import com.cnhis.cloudhealth.commons.dao.BaseDao;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hdd on 2016-11-02.
 */
@Repository
public class MobileInfusionDao extends BaseDao {
    /**
     * 查询病人列表信息 （输液待执行病人）
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> query1476p(Map<String, Object> map) throws Exception{
        List list = this.queryForList("MobileInfusionMapper.query1476p", map);
        Map<String, Object> infomap = new HashMap<String, Object>();
        infomap.put("list", list);
        return infomap;
    }

    /**
     * 查询病人列表信息 （输液 已完成  病人）
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> findCompletedPatientList(Map<String, Object> map) throws Exception{
        List list = this.queryForList("MobileInfusionMapper.findCompletedPatientList", map);
        Map<String, Object> infomap = new HashMap<String, Object>();
        infomap.put("list", list);
        return infomap;
    }

    /**
     * 查询病区科室
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDept() throws Exception{
        List list = this.queryForList("MobileInfusionMapper.queryDept", null);
        Map<String, Object> depmap = new HashMap<String, Object>();
        depmap.put("list", list);
        return depmap;
    }


    /**
     * 根据 就诊id  查询此病人的详细信息
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryDetailPatient(Map<String, Object> map) throws Exception{
        List list = this.getSqlSession().selectList("MobileInfusionMapper.queryDetailPatient",map);
        Map<String, Object> detmap = new HashMap<String, Object>();
        detmap.put("list", list);
        return detmap;
    }

    /**
     * 获取病人  执行项目
     * 待执行病人  （进行中的 项目）
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryExecutePro(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryExecutePro", map, currPage, pageSize);
    }

    /**
     * 获取病人  执行项目
     * 已完成  病人  （已完成的 项目）
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryCompleteExecutePro(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryCompleteExecutePro", map, currPage, pageSize);
    }

    /**
     * 获取病人  执行记录   也就是项目的明细
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryExecuteRecord(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryExecuteRecord", map, currPage, pageSize);
    }


    /**
     * 获取收费 记录
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryChargeRecord(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryChargeRecord", map, currPage, pageSize);
    }

    /**
     * 查询所有  待执行医嘱  信息
     * @param map
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryExcuseDoctor(Map<String, Object> map,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryExcuseDoctor", map, currPage, pageSize);
    }

    /**
     * 接单时  查询搜索的人员
     * 执行人  配药人  接单人
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryUser(Map<String, Object> paraMap)throws Exception{
        List list = this.queryForList("MobileInfusionMapper.queryUser", paraMap);
        Map<String, Object> usermap = new HashMap<String, Object>();
        usermap.put("list", list);
        return usermap;
    }

    /**
     *  查询  输液单，注射单 等 各个单据  用于打印
     * @return
     * @throws Exception
     */
    public Map<String,Object> querySingleinfusion(Map<String, Object> paramMap)throws Exception{
        List list = this.queryForList("MobileInfusionMapper.querySingleinfusion", paramMap);
        Map<String, Object> singlemap = new HashMap<String, Object>();
        singlemap.put("list", list);
        return singlemap;
    }

    /**
     * 根据 vaf_list 的值 查询视图 V_VAF_1
     * 循环 判断  vaf10 是否有小于8 的
     */
    public List searchVafView(List<Map<String, Object>> list) throws Exception {
        List viewlist = this.queryForList("MobileInfusionMapper.searchVafView", list);
        return viewlist;
    }


    public Map<String, Object> queryForPage(String sqlDef, Map<String, Object> param, int currPage, int pageSize) throws Exception {
        String getCountSqlDef = sqlDef + "Count";
        /*获取查询结果的记录总数*/
        int total = ((Integer)this.getSqlSession().selectOne(getCountSqlDef, param)).intValue();
        int pageIndex = currPage - 1;
        pageIndex = pageIndex < 0?0:pageIndex;
        int startNum1 = pageIndex * (pageSize < 1?1:pageSize);
        List list = this.getSqlSession().selectList(sqlDef, param, new RowBounds(startNum1, pageSize));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }


    /**
     * 点击执行完成 的操作
     * @param list
     * @return
     * @throws Exception
     */
    public String performOver(List<Map<String, Object>> list)throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",list);
        this.getSqlSession().update("MobileInfusionMapper.performOver",map);
        this.getSqlSession().update("MobileInfusionMapper.performOver2",map);
        return "yes";
    }

    /**
     * 查询 退费审核 列表
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryRefundAudit(Map<String, Object> paramMap,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryRefundAudit", paramMap, currPage, pageSize);
    }

    /**
     * 退费审核中的 人员搜索
     * @param paramMap
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryUserInRefundAudit(Map<String, Object> paramMap,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.queryUserInRefundAudit", paramMap, currPage, pageSize);
    }

    //保存  修改费用明细
    public void batchUpdateByVaf(Map<String,Object> map)throws Exception{
        this.getSqlSession().update("MobileInfusionMapper.updateVaj",map);
        this.getSqlSession().update("MobileInfusionMapper.updateVaj_2",map);
    }

    public void saveVbj1(Map<String,Object> map)throws Exception{
        this.getSqlSession().insert("MobileInfusionMapper.saveOrdervbj",map);
    }

    /**
     * 查询是否有未交费的
     * @param list
     * @return
     * @throws Exception
     */
    public Map<String,Object> searchChargeCount(List<Map<String, Object>> list)throws Exception{
        List baclist = new ArrayList();
        baclist= getSqlSession().selectList("MobileInfusionMapper.searchChargeCount",list);
        Map<String,Object> backmap = new HashMap<String,Object>();
        backmap.put("list",baclist);
        return backmap;
    }


    /**
     * 修改医嘱状态  修改为进行中
     * @param map
     * @throws Exception
     */
    public void updateVBI1(Map<String,Object> map)throws Exception{
        this.getSqlSession().update("MobileInfusionMapper.updateVBI1",map);
    }
    public void updateVBI1_2(Map<String,Object> map)throws Exception{
        this.getSqlSession().update("MobileInfusionMapper.updateVBI1_2",map);
    }


    /**
     * 根据科室查询  此科室下的所有人员
     * 退费审核
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String,Object> refundAuditByDepId(Map<String, Object> paramMap,int currPage, int pageSize)throws Exception{
        return queryForPage("MobileInfusionMapper.refundAuditByDepId", paramMap, currPage, pageSize);
    }

    /**
     * 点击皮试结果
     * @param paramMap
     * @return
     * @throws Exception
     */
    public Map<String,Object> skinTest(Map<String, Object> paramMap)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.skinTest",paramMap);
    }

    /**
     * 基本信息 保存皮试前使用
     * @param paramMap
     * @return
     */
    public List queryBaseInfo(Map<String, Object> paramMap)throws Exception{
        return getSqlSession().selectList("MobileInfusionMapper.queryBaseInfo",paramMap);
    }

    /**
     * 皮试的修改
     * 保存皮试信息   更新vaf
     * @param paramMap
     * @throws Exception
     */
    public void saveSkinTestResult(Map<String, Object> paramMap)throws Exception{
        getSqlSession().update("MobileInfusionMapper.saveSkinTestResult",paramMap);
    }

    /**
     * 查询是否具有过敏记录
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinRecord(Map<String, Object> paramMap)throws Exception{
        return getSqlSession().selectList("MobileInfusionMapper.queryIsHasSkinRecord",paramMap);
    }

    /**
     * 添加过敏记录
     * @param paramMap
     * @throws Exception
     */
    public void addSkinRecord(Map<String, Object> paramMap)throws Exception{
        getSqlSession().insert("MobileInfusionMapper.addSkinRecord",paramMap);
    }

    /**
     * 修改过敏记录
     * @param paramMap
     * @throws Exception
     */
    public void updateSkinRecord(Map<String, Object> paramMap)throws Exception{
        getSqlSession().update("MobileInfusionMapper.updateSkinRecord",paramMap);
    }

    /**
     * 查询是否具有过敏药物
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List queryIsHasSkinDrug(Map<String, Object> paramMap)throws Exception{
        return getSqlSession().selectList("MobileInfusionMapper.queryIsHasSkinDrug",paramMap);
    }

    /**
     * 添加过敏药物
     * @param paramMap
     * @throws Exception
     */
    public void addSkinDrug(Map<String, Object> paramMap)throws Exception{
        getSqlSession().insert("MobileInfusionMapper.addSkinDrug",paramMap);
    }

    /**
     * @Description:(病人退费申请)
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     */
    public Map<String,Object> findAppRefund(Map map) throws Exception {
        return (Map<String, Object>)this.queryToObject("MobileInfusionMapper.findAppRefund", map);
    }

    /**
     * @Description:(更新退费状态1)
     * @param '[map]'
     * @throws
     */
    public void refund1(Map map) throws Exception {
        update("MobileInfusionMapper.refund1",map);
    }

    /**
     * @Description:(更新退费状态2)
     * @param '[map]'
     * @throws
     */
    public void refund2(Map map) throws Exception {
        update("MobileInfusionMapper.refund2",map);
    }

    /**
     * //判断病人卡上钱是否够用
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryVai23(Map<String, Object> map)throws Exception{
        List list =  getSqlSession().selectList("MobileInfusionMapper.queryVai23",map);
        Map<String,Object> backmap = new HashMap<String,Object>();
        backmap.put("list",list);
        return backmap;
    }

    public Map<String,Object> queryVaiAll(Map<String, Object> map)throws Exception{
        List list =  getSqlSession().selectList("MobileInfusionMapper.queryVaiAll",map);
        Map<String,Object> backmap = new HashMap<String,Object>();
        backmap.put("alllist",list);
        return backmap;
    }



    //<!--查询赠送的金额-->
    public Map<String,Object> queryCountByVaa01(String vaa01)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.queryCountByVaa01",vaa01);
    }

    // <!--查询所有药品金额-->
    public Map<String,Object> queryVaj38ByVaa01(String vaa01)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.queryVaj38ByVaa01",vaa01);
    }


    /**
     * 根据 vaf01  查询vbj表  查询出  执行时间 vaj08  用于打印
     * @return
     * @throws Exception
     */
    public Map<String, Object> queryVaj08ByVaf01(Map<String, Object> paramMap)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.queryVaj08ByVaf01",paramMap);
    }

    /**
     * 查询病人 是不是会员
     * @throws Exception
     */
    public Map<String,Object> findMembers(String vaa01)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.findMembers",vaa01);
    }

    /**
     * 查询病人 会员卡类型
     */
    public Map<String,Object> findMemberCardType(String vaa01)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.findMemberCardType",vaa01);
    }

    /**
     * 根据 科室 和  vaf01 查询是否可以申请  是本科室给药途径退费
     * @return
     * @throws Exception
     */
    public Map<String,Object> queryApplyRefuse(Map<String, Object> paramMap)throws Exception{
        return getSqlSession().selectOne("MobileInfusionMapper.queryApplyRefuse",paramMap);
    }

    /**
     * 配液执行完成的操作
     * @param paramMap
     * @return
     * @throws Exception
     */
    public String technicExecToComplete(Map<String, Object> paramMap)throws Exception{
        this.getSqlSession().update("MobileInfusionMapper.performOver",paramMap);
        this.getSqlSession().update("MobileInfusionMapper.performOver2",paramMap);
        return "yes";
    }


    /**
     * 确认执行穿刺
     * @param paramMap
     * @return
     * @throws Exception
     */
    public String ohmobilesyworkinsert(Map<String, Object> paramMap)throws Exception{
         this.getSqlSession().insert("MobileInfusionMapper.ohmobilesyworkinsert",paramMap);
        return "yes";
    }


    /**
     * 判断是输液是否已经开始了  Mobile_SY_WORK
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List checkInfusionStart(Map<String,Object> paramMap)throws Exception{
        return  this.getSqlSession().selectList("MobileInfusionMapper.checkInfusionStart",paramMap);
    }

    /**
     * 查询 oldvbi21  是否正在输液
     * @param paramMap
     * @return
     * @throws Exception
     */
    public String queryoldVBI21(Map<String,Object> paramMap) throws Exception{
        String oldvbi21 = this.getSqlSession().selectOne("MobileInfusionMapper.queryoldVBI21",paramMap);
        return oldvbi21;
    }

    /**
     * 判断是否已经输液完成
     * @param paramMap
     * @return
     * @throws Exception
     */
    public List checkInfusionEnd(Map<String,Object> paramMap)throws Exception{
        return  this.getSqlSession().selectList("MobileInfusionMapper.checkInfusionEnd",paramMap);
    }

    /**
     * 不换瓶子 的保存方法
     * @param paramMap
     * @throws Exception
     */
    public void insertSywFlg0(Map<String,Object> paramMap)throws Exception{
       this.getSqlSession().insert("MobileInfusionMapper.insertSywFlg0",paramMap);
    }




}
